<?php


namespace app\api\controller\v39;


use app\api\controller\v2\Auth;
use app\common\cachemodel\chat\HzQuestion;
use app\common\cachemodel\chat\HzQuestionText;
use app\common\cachemodel\Recode as RecodeModel;
use app\common\cachemodel\zl5\Zl5ExplainChat;
use app\common\cachemodel\zl5\Zl5ExplainContent;
use app\common\cachemodel\zl5\Zl5ExplainTitle;
use app\common\cachemodel\zl5\Zl5ExplainType;
use app\common\lib\Util;
use app\common\service\ChatParse;
use think\Db;

class NatalChat extends Auth
{
    /**
     * 提问
     */
    public function askQuestions()
    {
        $qid = input('qid/d', 0);
        $rid = input('rid/d', 0);
        $qtype = input('qtype/d', 0);

        $recodeInfo = (new RecodeModel())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常，档案不存在～');
        }

        if (!in_array($qtype, [1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $zl5ExplainChatModel = new Zl5ExplainChat();
        $hzQuestionConfig = config('hz_question');
        if ($qtype === 1)
        {
            $zl5ExplainTitleModel = new Zl5ExplainTitle();
            $questionInfo = $zl5ExplainTitleModel->getInfoById($qid);
            if (!$questionInfo)
            {
                return show(config('code.error'), '服务异常，请稍后再试～');
            }
            // 去结果的 依据
            $basis = Util::convertNatalBasisRes($recodeInfo, $questionInfo['basis_easy']);

            $zl5ExplainContentModel = new Zl5ExplainContent();
            $contentInfo = $zl5ExplainContentModel->getExplainContentInfo($qid, $basis);

            $extend = [
                'type_id2' => $zl5ExplainTitleModel->getQuestionType($qid)
            ];
            $insertData = [
                'status' => 1,
                'uid' => $this->user['id'],
                'rid' => $rid,
                'qtype' => 1,
                'type_id' => $questionInfo['type_id'],
                'title_id' => $qid,
                'content_id' => $contentInfo['id'],
                'state' => 0,
                'extend' => json_encode($extend, JSON_UNESCAPED_UNICODE),
                'create_time' => $this->request->time(),
            ];
            $chatId = $zl5ExplainChatModel->SaveData($insertData);

            $zl5ExplainTitleModel->where(['id' => $qid])->setInc('question_num');

            $linkList = [
                [
                    'type' => 'basis',
                    'title' => $zl5ExplainContentModel->convertBasisToText($basis),
                    'basis' => $basis,
                    'target' => '',
                    'link' => '',
                ]
            ];
            if (isset($questionInfo['extend']['recommend_goods']))
            {
                foreach ($questionInfo['extend']['recommend_goods'] as $recommendGoodtem)
                {
                    $linkList[] = [
                        'type' => 'shop',
                        'title' => $recommendGoodtem['title'],
                        'basis' => '',
                        'target' => $recommendGoodtem['target'],
                        'link' => $recommendGoodtem['link'],
                    ];
                }
            }

            $type = $zl5ExplainTitleModel->getQuestionType($qid);
            $resData = [
                'chat_id' => $chatId,
                'title' => $hzQuestionConfig[$type],
                'question' => $questionInfo['title'],
                'content' => $contentInfo['content'],
                'link_list' => $linkList,
                'state' => 0,
                'create_time' => '刚刚',
            ];
        } else
        {
            $question = (new HzQuestion())->getInfoById($qid);
            // 用户提问问题  没有 免档案的（12星座问题）
            $resultTextData = ChatParse::getAnalysisResult($question, '', $recodeInfo);
            $extend = [
                'result_data' => $resultTextData
            ];
            $insertData = [
                'status' => 1,
                'uid' => $this->user['id'],
                'rid' => $rid,
                'qtype' => 2,
                'type_id' => $question['question_type'],
                'title_id' => $qid,
                'content_id' => 0,
                'state' => 0,
                'extend' => json_encode($extend, JSON_UNESCAPED_UNICODE),
                'create_time' => $this->request->time(),
            ];
            $chatId = $zl5ExplainChatModel->SaveData($insertData);

            $resData = [
                'chat_id' => $chatId,
                'title' => $hzQuestionConfig[$question['question_type']],
                'question' => $question['question'],
                'content' => $zl5ExplainChatModel->getChatResultDataText($resultTextData),
                'link_list' => [],
                'state' => 0,
                'create_time' => '刚刚',
            ];
        }

        return show(config('code.success'), '请求成功', $resData);
    }
}
